<template>
  <div class="h-full w-full flex items-center justify-center bg-hex-000">Loading...</div>
</template>

<script setup  lang='ts'>
import { useAddressStore } from '@/stores/address'

const auth = 'scope.userLocation'
const address = useAddressStore()
const choose_location = () => {
  let config = {}
  if (JSON.stringify(optionsObj) !== '{}') {
    config = optionsObj
  }
  uni.chooseLocation({
    ...config,
    success: res => {
      address.updateLocation({
        latitude: res.latitude,
        longitude: res.longitude,
        detailAddress: `${res.address.substring(address.locationInfo.province.length + address.locationInfo.city.length)}${res.name}`,
      })
      setTimeout(() => {
        uni.navigateBack()
      }, 500)
    },
    fail: res => {
      // 取消则自动返回、则显示错误
      if (res.errMsg.includes('cancel')) {
        uni.navigateBack()
      }
    },
  })
}
let optionsObj = {}
onLoad((options) => {
  if (options) {
    optionsObj = options
  }
})
onMounted(() => {
  // #ifdef MP-WEIXIN || MP-BAIDU || MP-TOUTIAO || MP-QQ
  uni.getSetting({
    success(res) {
      if (!res.authSetting[auth]) {
        uni.authorize({
          scope: auth,
          success(res) {
            choose_location()
          },
          fail: res => {
            console.log(res)
          },
        })
      } else {
        choose_location()
      }
    },
    fail: res => {
      console.log('请先获取授权')
    },
  })
  // #endif
  // #ifdef MP-ALIPAY || H5 || APP
  choose_location()
  // #endif
})
</script>
